/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.gwt.basics.manager.impl;

import com.genia.toolbox.web.gwt.basics.bean.FileInformation;
import com.genia.toolbox.web.gwt.basics.context.GwtBasicsContext;
import com.genia.toolbox.web.gwt.basics.manager.GwtFileUploadManager;

/**
 * implementation of {@link GwtFileUploadManager}.
 */
public class GwtFileUploadManagerImpl
    implements GwtFileUploadManager
{
  /**
   * the current identifier to to be used in the client-server communication.
   */
  private static Long nextIdentifier = 1l;

  /**
   * the context of the request.
   */
  private GwtBasicsContext context;



  /**
   * return a new identifier to be used in the client-server communication.
   * 
   * @return a new identifier to be used in the client-server communication
   * @see com.genia.toolbox.web.gwt.basics.manager.GwtFileUploadManager#getNewIdentifier()
   */
  public Long getNewIdentifier()
  {
    synchronized (GwtFileUploadManager.class) {
      return nextIdentifier++;
    }
  }



  /**
   * get the progress of the upload of a file.
   * 
   * @param id
   *          the identifier of the file to monitor.
   * @return the progress of upload of the file (a number between 0 and 100)
   * @see com.genia.toolbox.web.gwt.basics.manager.GwtFileUploadManager#getUploadProgress(java.lang.Long)
   */
  public Integer getUploadProgress(Long id)
  {
    Integer res = getContext().getProgress(id);
    if (res == null) {
      return 0;
    }
    return res;
  }



  /**
   * getter for the context property.
   * 
   * @return the context
   */
  public GwtBasicsContext getContext()
  {
    return context;
  }



  /**
   * setter for the context property.
   * 
   * @param context
   *          the context to set
   */
  public void setContext(GwtBasicsContext context)
  {
    this.context = context;
  }



  /**
   * returns the {@link FileInformation} associated to a file.
   * 
   * @param id
   *          the identifier of the file which informations must be returned
   * @return the {@link FileInformation} associated to a file *
   * @see com.genia.toolbox.web.gwt.basics.manager.GwtFileUploadManager#getFileInformation(java.lang.Long)
   */
  public FileInformation getFileInformation(Long id)
  {
    return getContext().getFileInformation(id);
  }



  /**
   * register a new {@link FileInformation}.
   * 
   * @param fileInformation
   *          the {@link FileInformation} to register
   * @return the session identifier of the refistered {@link FileInformation}
   * @see com.genia.toolbox.web.gwt.basics.manager.GwtFileUploadManager#registerFileInformation(com.genia.toolbox.web.gwt.basics.bean.FileInformation)
   */
  public Long registerFileInformation(FileInformation fileInformation)
  {
    Long res = fileInformation.getGwtFileInformation().getSessionIdentifier();
    if (res == null) {
      if (fileInformation.getPermanentIdentifier() != null) {
        res = getContext().getSessionIdentifierForPermanentIdentifier(fileInformation.getPermanentIdentifier());
      }
      if (res == null) {
        res = getNewIdentifier();
      }
      fileInformation.getGwtFileInformation().setSessionIdentifier(res);
    }
    getContext().setFileInformation(fileInformation);
    return res;
  }
}
